import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import devtools from "vite-plugin-vue-devtools";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { platformResultName } from "./src/enums/platform";
import type { ProxyBaseURL } from "./src/types/multiStore";

export default defineConfig(({ mode }) => {
  // 配置代理
  const env = loadEnv(mode, process.cwd() + "/env");
  const newRewirteProxy = (PLATFORM: ProxyBaseURL) => {
    return { target: env[`VITE_${PLATFORM}_URL`], changeOrigin: true };
  };
  const proxy = {};
  Object.keys(platformResultName).forEach((k) => {
    proxy[`/${platformResultName[k]}`] = newRewirteProxy(platformResultName[k]);
  });

  // 返回配置
  return {
    server: { host: "0.0.0.0", port: 1250, cors: true, proxy },
    plugins: [
      vue(),
      devtools(),
      AutoImport({ resolvers: [ElementPlusResolver()], dts: "src/types/zombies/auto-imports.d.ts"}),
      Components({ resolvers: [ElementPlusResolver()], dts: "src/types/zombies/components.d.ts"}),
    ],
    envDir: "./env",
    resolve: { alias: { "@": "/src" } },
  };
});
